﻿/*********************************************************************
 * Módulo 2. Entorno de Trabajo.
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import com.eclecticdesignstudio.motion.Actuate;
import com.eclecticdesignstudio.motion.easing.Elastic;
import nme.display.Bitmap;
import nme.display.Sprite;
import nme.display.StageAlign;
import nme.display.StageScaleMode;
import nme.media.Sound;
import nme.events.Event;
import nme.Assets;
import nme.Lib;

/****************************************
 * Código fuente adaptado de 
 * http://www.nme.io/developers/tutorials
 * Autor Original: Joshua Granick
 ****************************************/

class HolaMundoNME extends Sprite {	
	
  private var logo:Bitmap;
  private var sound:Sound;
	
  public function new () {		
    /* Delega en el constructor de Sprite (clase padre) */
    super ();
    /* Define el manejador de entrada */
    addEventListener (Event.ADDED_TO_STAGE, this_onAddedToStage);
  }
		
  private function construct ():Void {		
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;

    /* Carga el logo */
    logo = new Bitmap (Assets.getBitmapData ("assets/tegnix.png"));
		
    /* Centra el logo en el centro de la pantalla con transparencia a 0*/
    logo.x = (stage.stageWidth - logo.width) / 2;
    logo.y = (stage.stageHeight - logo.height) / 2;
    logo.alpha = 0;
		
    /* Añade el logo a la display list */ 
    addChild (logo);

    /* Obtiene el sonido y lo reproduce*/
    sound = Assets.getSound ("assets/music/elvuelodelmoscardon.ogg");
    sound.play ();
		
    /* Delega la animación en la biblioteca Actuate */
    Actuate.tween (logo, 2, { alpha: 1 } ).onComplete (moveLogo);		
  }
	
  /* Movimiento aleatorio del logo */
  private function moveLogo ():Void {		
    var randomX = Math.random () * (stage.stageWidth - logo.width);
    var randomY = Math.random () * (stage.stageHeight - logo.height);
    Actuate.tween (logo, 2, { x: randomX, y: randomY } ).ease (Elastic.easeOut).onComplete (moveLogo);		
  }

  /* Manejadores de eventos */
  private function this_onAddedToStage (event:Event):Void {
    construct ();
  }

  /* Punto de entrada */
  public static function main () {		
    Lib.current.addChild (new HolaMundoNME ());
  }	
	
}
